﻿@echo off

set _Self=%0

if "%1" == "" goto :ERROR
set _DestFile=%1
shift

if "%1" == "" goto :ERROR
set _VersionDir=%1
shift

if "%1" == "" goto :ERROR

if "%1" == "/on" set _Order=ASC
if "%1" == "/o-n" set _Order=DESC
if not "%_Order%" == "" shift

if "%_Order%" == "DESC" (
	echo   FROM "%_VersionDir%" TO "%_DestFile%" - desc
) else (
	echo   FROM "%_VersionDir%" TO "%_DestFile%" - asc
)

:LOOP
if exist %_VersionDir%\%1 goto MERGE
rem echo   MERGE - %1 - miss
goto CONTINUE

:MERGE
echo   MERGE - %1

if "%_Order%" == "DESC" (
	for /f "delims=;" %%s in ('dir %_VersionDir%\%1\*.sql /s /b /o-n') do copy %_DestFile% + "%%s" %_DestFile% > nul
) else (
	for /f "delims=;" %%s in ('dir %_VersionDir%\%1\*.sql /s /b /on') do copy %_DestFile% + "%%s" %_DestFile% > nul
)

:CONTINUE
shift
if "%1" == "" goto :END
goto :LOOP

:ERROR
echo   ERROR!!
echo   call me as: %_Self% DestFile VersionDir [Order] SubDir1 [SubDir2 [SubDir3...]]

:END
set _Self=
set _DestFile=
set _VersionDir=
set _Order=
echo.
